package cn.the.angry.util;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import cn.the.angry.resourcemanager.ResourceManager;

public class GraphicUtils {
	public static final int AUTOFILLVERTICAL = 0x77770001;
	public static final int AUTOFILLHORIZONTAL = 0x77770002;
	
	
	public static void autoFill(Graphics2D g2, BufferedImage image,int x, int y, int canvasW, int canvasH, int autofillflag){
		int i = 0;
		int w = image.getWidth(null)-2;
		int h = image.getHeight(null)-2;
		if(autofillflag == AUTOFILLHORIZONTAL){
			
			while(canvasW-w*i > w){
				g2.drawImage(image, w*(i++), y, null);
			}
			g2.drawImage(ResourceManager.getImageClip(image, 0, 0, canvasW-w*i, h), w*i, y, null);
		}
		if(autofillflag == AUTOFILLVERTICAL){
			while(canvasH-h*i > h){
				g2.drawImage(image, x, h*(i++), null);
			}
			g2.drawImage(ResourceManager.getImageClip(image, 0, 0, w, canvasH-h*i), x, h*i, null);
		}
		
	}
	
	public static BufferedImage rotateImage(final BufferedImage bufferedimage, final int width, final int height, float degree) {
		int w = (int)Math.sqrt(width*width+height*height);
//        int w = width > height ? width : height;
        int h = w;
        int type = bufferedimage.getColorModel().getTransparency();
        		    
		BufferedImage img;
		Graphics2D graphics2d;
		(graphics2d = (img = new BufferedImage(w, h, type))
		        .createGraphics()).setRenderingHint(
		        RenderingHints.KEY_INTERPOLATION,
		        RenderingHints.VALUE_INTERPOLATION_BILINEAR);
         
/*         Graphics2D tempG2d=img.createGraphics();
         tempG2d.setColor(Color.BLUE);
         tempG2d.fillRect(0, 0, w, h);*/
         graphics2d.rotate(degree, w / 2, h / 2);
//         graphics2d.fillRect(0, 0, w, h);
         graphics2d.drawImage(bufferedimage, (w-width)/2, (h-height)/2, null);
         graphics2d.dispose();
        return img;
     }
	
	public static BufferedImage zoomOut(final BufferedImage image, final int zoomOutScale){
		final int width=image.getWidth(), height=image.getHeight();
		BufferedImage buf = new BufferedImage(width/zoomOutScale, height/zoomOutScale, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2 = buf.createGraphics();
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2.drawImage(image, 0, 0, width/zoomOutScale, height/zoomOutScale, null);
		g2.dispose();
		return buf;
		
	}
}
